package com.icim.history;

import static com.icim.define.CommDefine._NORMAL;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.icim.common.LoadHistoryConfig;
import com.icim.exception.LoadMainException;
import com.icim.rabbitmq.util.RabbitmqManager;
import com.icim.service.ILoadMainService;
import com.icim.service.impl.LoadMainService;

public class SCMHistoryHandler {
	private Logger logger = Logger.getLogger(SCMHistoryHandler.class);
	@Resource
	RabbitmqManager rabbitmqManager;
	
	private LoadMainService loadMainService;

	public static void main(String[] args) {
		SCMHistoryHandler test = new SCMHistoryHandler();
		
		String message = "<transaction><trans_cnt>1</trans_cnt><table><operation>INSERT</operation><pv_unq_seq_id>AIQM2PUG7TW3IRD9RMP3SBD5Z</pv_unq_seq_id><data_seq_id>9N9QSEXPWB8268K2YBBD1ZBGJ</data_seq_id><unq_seq_id>7CQWLUKG93AVJUCCZF6GLND11</unq_seq_id><data_cate>TES2</data_cate><data_id>1</data_id><data_ext>1</data_ext><data_item>1</data_item><data_desc>4</data_desc><ope_tbl_name>BIS_DATA</ope_tbl_name><sta_time_d>41574.2750652067</sta_time_d><ope_evt_name>XplstdatService</ope_evt_name><ope_evt_node>127.0.0.1</ope_evt_node><ope_evt_usr>opitest01</ope_evt_usr><ope_evt_no>123</ope_evt_no></table></transaction>";
//		test.onMessage(message);

	}

//	public void onMessage(String message) {
//
//		long lRc = _NORMAL;
//		String historyErrQueueName = loadHistoryConfig.getHistoryErrQueueName();
//		logger.info("InTrx: " + message);
//		if (message == null) {
//			return;
//		}
//		/******
//		 * 执行抄HIS的逻辑,以LoadMainService为实现类
//		 * ******/
//		lRc = loadMainService.SubMainProc(message);
//		if (lRc == _NORMAL) {
//			logger.info("执行成功,主逻辑返回码:" + lRc);
//		} else {
//			logger.info("执行失败,主逻辑返回码:" + lRc);
//		}
//
//		/******
//		 * 抄送HIS失败，则放入ERR_Q中
//		 * **********/
//		if (lRc != _NORMAL) {
//			rabbitmqManager.setMessage(historyErrQueueName, message);
//		}
//
//	}
	public void runAsWatchDog(){
			long lRc;
			String historyQueueName = loadMainService.getHisQueueName();
			String historyErrQueueName = loadMainService.getHisErrorQueueName();
			String message;
			StringBuffer messageBuffer = new StringBuffer();
			StringBuffer logBuffer = new StringBuffer();
			while (true) {
				lRc = _NORMAL;
				message = rabbitmqManager.recieveMessage(historyQueueName);
				if (message == null) {
					break;
				}
				messageBuffer.setLength(0);
				messageBuffer.append("InTrx: ").append(message);
				logger.info(messageBuffer);
				/******
				 * 鎵ц鎶凟RROR Q鐨勯�杈�浠oadMainService涓哄疄鐜扮被
				 * ******/
				try {
					lRc = loadMainService.SubMainProc(message);
				}catch(LoadMainException e){
					lRc = 1; 
					logger.error(e.toString());
				}catch (Exception e) {
					lRc = 1;
					logger.error(e.toString());
				}
				if (lRc != _NORMAL) {
//					logBuffer.setLength(0);
//					logBuffer.append("xml handle success,return code:").append(lRc);
//					logger.info(logBuffer);
//				} else {
					logBuffer.setLength(0);
					logBuffer.append("xml handle failed,error code:").append(lRc);
					logger.error(logBuffer.toString());
				}

				if (lRc != _NORMAL) {
					rabbitmqManager.setMessage(historyErrQueueName, message);
					logger.error("message has benn send to error queue");
				}
			}
		logger.info("["+loadMainService.getHisQueueName()+"] Application run as watch dog ..........");
	}

	public LoadMainService getLoadMainService() {
		return loadMainService;
	}

	public void setLoadMainService(LoadMainService loadMainService) {
		this.loadMainService = loadMainService;
	}

	
	
}
